const key ='JB3BZ-OLZCG-NSTQ2-IY7AB-LXSG2-SUFZN';
const config = require(`../config.js`);
const host = `${config.url}/admin/`; //这个是你们的接口域名
let onOff  = true
function promise(url, params, method) {

  // wx.showLoading({
  //   title: '加载中...'
  // })

  return new Promise((resolve, reject) => {
    wx.request({
      url: `${url}`,
      data: params,
      method: method,
      header: {
        'content-type': 'application/json',
        //每次从本地获取toekn传给后台
        'jtxs': wx.getStorageSync("token")
      },
      complete: (res) => {
        if (res.data.login == false) {
          if (wx.getStorageSync("token") != "") {

            wx.login({
              success: rest => {
                wx.request({
                  url: `${config.url}/admin/user/login`,
                  data: {
                    wxCode: rest.code,
                    loginType: '1',
                  },
                  header: {
                    'content-type': 'application/json',
                  },
                  method: 'POST',
                  success: function (res) {

                    if (res.data.result == true) {
                      if (res.data.code == 99) {
                        //本地存储
                        wx.setStorageSync("token", res.data.data)
                        //城市Id
                        //codeNum
                        wx.setStorageSync("codeNum", res.data.code)
                        wx.setStorageSync("userMess", res.data.object)
                        wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                        wx.setStorageSync("phone", res.data.object.phone)
                        wx.setStorageSync("openId", res.data.body.openid)
                        // wx.reLaunch({
                        //   url: '../index/index',
                        // })
                      }
                    } else {
                      if (res.data.code == 103 || res.data.code == 105) {
                        //本地存储
                        wx.setStorageSync("token", res.data.data)
                        wx.setStorageSync("phone", res.data.object.phone)
                        wx.setStorageSync("openId", res.data.body.openid)
                        //城市Id
                        //codeNum
                        wx.setStorageSync("codeNum", res.data.code)
                        wx.setStorageSync("userMess", res.data.object)
                        wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                        // wx.navigateTo({

                        //   url: '../type/type',
                        // })
                      }
                      else if (res.data.code == 104) {
                        //本地存储
                        wx.setStorageSync("token", res.data.data)
                        wx.setStorageSync("phone", res.data.object.phone)
                        wx.setStorageSync("openId", res.data.body.openid)
                        //城市Id
                        //codeNum
                        wx.setStorageSync("codeNum", res.data.code)
                        wx.setStorageSync("userMess", res.data.object)
                        wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                        // wx.navigateTo({

                        //   url: '../shopBack/shopBack',
                        // })
                      }
                    }
                  }
                })
                return
              }
            })

          }
        }
        if (wx.getStorageSync("phone") != "" && wx.getStorageSync("cityId") != "" && onOff) {
          onOff = false
          wx.request({
            url: `${config.url}/admin/user/getUserInfo`,
            method: 'POST',
            data: {
              phone: wx.getStorageSync("phone"),
              cityId: wx.getStorageSync("cityId"),
            },
            header: {
              'content-type': 'application/json',
              //每次从本地获取toekn传给后台
              'jtxs': wx.getStorageSync("token")
            },
            // authFunction 授权功能 0、支付 1、收货 2、售后 3、押金
            success: function (res) {
              if (res.data.result) {
                if( res.data.data == "0"){
                  wx.setStorageSync('authFunction', res.data.body.authUserType)
                }else{
                  wx.removeStorageSync('authFunction')
                }
                wx.setStorageSync("storeName", res.data.body.name)
                wx.setStorageSync("codeNum", res.data.body.code)
                wx.setStorageSync("type", res.data.data)
                if (res.data.object){
                  wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                  wx.setStorageSync("openId", res.data.object.openid)
                }
                setTimeout(() => onOff = true, 2000)
              }
            }
          })
        }
        resolve(res)
      },
      fail: reject
    })

  })

}


function roleSwitch (obj) {
  obj.salesPrice = obj.personalPrice || 0
  obj.salesPriceVip = obj.personalPriceVip || 0
}



module.exports = {
  // 价格切换
  roleSwitch : roleSwitch,
  // -------------------------------------详情页
  //支付
  pay: function (params) {
    return promise(host + 'wx/pay/unifiedOrder', params, 'POST')
      .then(res => res.data)
  },

  //店铺类型
  shopType: function (params) {
    return promise(host + 'shopInfoEntity/getShopDicData/', params, 'POST')
      .then(res => res.data)
  },


  // 店铺表单
  formSubmit: function (params) {
    return promise(host + 'shopInfoEntity/save', params, 'POST')
      .then(res => res.data)
  },
  //商铺地址管理
  handleAddr: function (params) {
    return promise(host + 'addressInfoEntity/getAddressInfoList', params, 'POST')
      .then(res => res.data)
  },
  //商铺地址管理默认设置
  setDefault: function (params) {
    return promise(host + 'addressInfoEntity/setDefault', params, 'POST')
      .then(res => res.data)
  },
  //订单分类
  orders: function (params) {
    return promise(host + 'orderInfoEntity/getStatusCount?openId=' + wx.getStorageSync("openId"), params, 'POST')
      .then(res => res.data)
  },
  //我的地址管理(个人需替换)
  myAddr: function (params) {
    return promise(host + 'addressInfoEntity/getAddressInfoList?userId=' + wx.getStorageSync("openId"), params, 'POST')
      .then(res => res.data)
  },

  //京天运费
  freight: function (params) {
    return promise(host + 'settingBaseEntity/getAmount?cityId=' + wx.getStorageSync("cityId"), params, 'POST')
      .then(res => res.data)
  },


  goodsList: function (params) {
    return promise(host + 'goodsInfoEntity/getGoodsIndex', params, 'POST')
      .then(res => res.data)
  },



  // 首页banner轮播图和公告-----imgUrls-----noticeList
  bannerList: function (params) {
    return promise(host + 'wxBannerEntity/getAll', params, 'POST')
      .then(res => res.data)
  },

  //地址
  cityList: function (params) {
    return promise(host + 'cityInfoEntity/getPage', params, 'POST')
      .then(res => res.data)
  },

  //首页自动获取地址
  cityId: function (params) {
    return promise(host + 'cityInfoEntity/getOneByName', params, 'POST')
      .then(res => res.data)
  },

  //首页电话 （城市电话  客户经理）
  phoneList: function (params) {
    return promise(host + 'cityInfoEntity/getByCityId', params, 'POST')
      .then(res => res.data)
  },

  //首页商品购物车
  goodsNumber: function (params) {
    return promise(host + 'shopCarEntity/saveShop', params, 'POST')
      .then(res => res.data)
  },

  //首页公告
  noticeList: function (params) {
    return promise(host + 'sysNoticeEntity/getAll', params, 'POST')
      .then(res => res.data)
  },

  //获取购物车内商品数量
  cutDetails: function (params) {
    return promise(host + 'shopCarEntity/shopQuantity', params, 'POST')
      .then(res => res.data)
  },


  //------------------------------------常用清单界面---------------------------------------------------------------------
  //我的清单-------城市热卖
  right_list: function (params) {
    return promise(host + 'goodsTypeEntity/getListByCityId', params, 'POST')
      .then(res => res.data)
  },
  //商品展示列表
  xdgoodsList: function (params) {
    return promise(host + 'goodsInfoEntity/getGoodsByCityId', params, 'POST')
      .then(res => res.data)
  },

  // 购物车页面------------------------
  // 获取所有购物车相关信息
  /**
   * @param {params} 城市/openid 
   */
  get_cart: function (params) {
    return promise(host + 'shopCarEntity/shopForm', params, 'POST')
  },
  // 设置数量
  /**
   * @param {params} 城市编号cityId 用户id userId 商品名称goodsName 商品明细id goodsDetailsId 数量amount 单价price 总价totalPrice
   */
  set_amount: function (params) {
    return promise(host + 'shopCarEntity/saveShop', params, 'POST')
      .then(res => res.data)
  },
  // 分类页面-----------------------
  // 获取分类
  /**
   */

  getType: function (params) {
    return promise(host + 'goodsInfoEntity/getGoodsType', params, 'POST')
      .then(res => res.data)
  },
  // 获取商品
  /**
   * rank类级，id类的id，cityId城市id
   */
  getGoods: function (params) {
    return promise(host + 'goodsInfoEntity/getGoods', params, 'POST')
      .then(res => res.data)
  },
  // 评论列表
  conmentList: function (params) {
    return promise(host + 'goodsInfoEntity/getGoodsEval', params, 'POST')
      .then(res => res.data)
  },

  // 商城详情
  goodsDetails: function (params) {
    return promise(host + 'goodsInfoEntity/getGoodsDetails', params, 'POST')
      .then(res => res.data)
  },
  // 商品切换
  cutDetails: function (params) {
    return promise(host + 'shopCarEntity/shopQuantity', params, 'POST')
      .then(res => res.data)
  },

  //商品评论图片上传
  uploadImg: function (params) {
    return promise(host + 'goodsEvalEntity/upload', params, 'POST')
      .then(res => res.data)
  },

  //钱包充值
  topUp: function (params) {
    return promise(host + 'orderInfoEntity/topUp', params, 'POST')
      .then(res => res.data)
  },
  //充值回调
  topUpConfirm: function (params) {
    return promise(host + 'orderInfoEntity/topUpConfirm', params, 'POST')
      .then(res => res.data)
  },
  //支付前验证
  saveOrderInfo: function (params) {
    return promise(host + 'orderInfoEntity/saveOrderInfo', params, 'POST')
      .then(res => res.data)
  },


  //评论商品
  saveEval: function (params) {
    return promise(host + 'goodsEvalEntity/saveEval', params, 'POST')
      .then(res => res.data)
  },

  //商品详情 添加清单
  insertSelective: function (params) {
    return promise(host + 'goodsCollectionEntity/insertSelective', params, 'POST')
      .then(res => res.data)
  },


  //购物车 单选多选
  editFlag: function (params) {
    return promise(host + 'shopCarEntity/editFlag', params, 'POST')
      .then(res => res.data)
  },


  //我的订单
  getIPage: function (params) {
    return promise(host + 'orderInfoEntity/getIPage', params, 'POST')
      .then(res => res.data)
  },


  //押金列表
  depositList: function (params) {
    return promise(host + 'orderDepositEntity/getIPage', params, 'POST')
      .then(res => res.data)
  },


  //退押金
  orderRefund: function (params) {
    return promise(host + 'orderDepositEntity/orderRefund', params, 'POST')
      .then(res => res.data)
  },



  //我的清单 取消关注 删除
  delExample: function (params) {
    return promise(host + 'goodsCollectionEntity/delExample', params, 'POST')
      .then(res => res.data)
  },

  //充值列表
  getAll: function (params) {
    return promise(host + 'settingRechargeAmountEntity/getAll', params, 'POST')
      .then(res => res.data)
  },


  //售后 退货
  orderRefund1: function (params) {
    return promise(host + 'orderReturnGoodsEntity/orderRefund', params, 'POST')
      .then(res => res.data)
  },

  //订单详情
  getOrderInfoDetails: function (params) {
    return promise(host + 'orderInfoEntity/getOrderInfoDetails', params, 'POST')
      .then(res => res.data)
  },
  //订单详情 确认收货
  isReceipt: function (params) {
    return promise(host + 'orderInfoEntity/isReceipt', params, 'POST')
      .then(res => res.data)
  },

  //订单详情 待配送撤销订单
  orderRefund2: function (params) {
    return promise(host + 'orderReturnGoodsEntity/orderRefund', params, 'POST')
      .then(res => res.data)
  },
  //订单详情 待支付撤销订单
  orderRefund3: function (params) {
    return promise(host + 'orderInfoEntity/editStatus', params, 'POST')
      .then(res => res.data)
  },
  //订单详情 售后
  verification: function (params) {
    return promise(host + 'orderReturnGoodsEntity/verification', params, 'POST')
      .then(res => res.data)
  },
  //确认收货 支付
  deliveryAndOrder: function (params) {
    return promise(host + 'orderInfoEntity/deliveryAndOrder', params, 'POST')
      .then(res => res.data)
  },

  //我的 电子账单
  getIPageRecord: function (params) {
    return promise(host + 'userMoneyRecordEntity/getIPage', params, 'POST')
      .then(res => res.data)
  },
  // 搜索
  searchIndex: function (params) {
    return promise(host + 'goodsSearchEntity/getGoodsIPage', params, 'POST')
      .then(res => res.data)
  },
  //商品品牌查询
  getGoodsBrand: function (params) {
    return promise(host + 'goodsSearchEntity/getGoodsBrand', params, 'POST')
      .then(res => res.data)
  },

  //待支付订单付款
  goToPay: function (params) {
    return promise(host + 'orderInfoEntity/goToPay', params, 'POST')
      .then(res => res.data)
  },
  //地址回显状态
  shopBack: function (params) {
    return promise(host + 'shopInfoEntity/getShopInfo', params, 'POST')
      .then(res => res.data)
  },
  //传店铺id查店铺详情
  shopDetails: function (params) {
    return promise(host + 'shopInfoEntity/getShopInfoDetails', params, 'POST')
      .then(res => res.data)
  },
  //首页公告详情
  noticeForm: function (params) {
    return promise(host + 'sysNoticeEntity/form', params, 'POST')
      .then(res => res.data)
  },

  //微信支付成功回调
  payDirect: function (params) {
    return promise(host + 'orderInfoEntity/payDirect', params, 'POST')
      .then(res => res.data)
  },
  //角标
  getStatus: function (params) {
    return promise(host + 'orderInfoEntity/getStatus', params, 'POST')
      .then(res => res.data)
  },
  //首页活动专场
  getDefault: function (params) {
    return promise(host + 'offFieldEntity/getDefault', params, 'POST')
      .then(res => res.data)
  },
  //首页底部导航
  navList: function (params) {
    return promise(host + 'goodsTypeEntity/getFirstAll', params, 'POST')
      .then(res => res.data)
  },
  //售后记录
  selectExceptAll: function (params) {
    return promise(host + 'orderReturnGoodsEntity/selectExceptAll', params, 'POST')
      .then(res => res.data)
  },
  //售后记录详情
  getById: function (params) {
    return promise(host + 'orderReturnGoodsEntity/getById', params, 'POST')
      .then(res => res.data)
  },
  //商品评价
  checkEval: function (params) {
    return promise(host + 'goodsEvalEntity/checkEval', params, 'POST')
      .then(res => res.data)
  },
  //给他人充值
  getUserByPhone: function (params) {
    return promise(host + 'user/getUserByPhone', params, 'POST')
      .then(res => res.data)
  },

  //供应商提交
  insertSelective1: function (params) {
    return promise(host + 'recruitTraderEntity/insertSelective', params, 'POST')
      .then(res => res.data)
  },

  //合伙人
  insertSelective2: function (params) {
    return promise(host + 'recruitPartnerEntity/insertSelective', params, 'POST')
      .then(res => res.data)
  },

  //常见问题列表
  selectByExample: function (params) {
    return promise(host + 'commonQuestionEntity/selectByExample', params, 'POST')
      .then(res => res.data)
  },

  //常见问题详情
  problemDetails: function (params) {
    return promise(host + 'commonQuestionEntity/form', params, 'POST')
      .then(res => res.data)
  },


  //提报需求列表
  demandList: function (params) {
    return promise(host + 'submitNeedEntity/selectByExampleForPage', params, 'POST')
      .then(res => res.data)
  },

  //提报需求分类列表
  demandClassList: function (params) {
    return promise(host + 'goodsTypeEntity/getOnceList', params, 'POST')
      .then(res => res.data)
  },

  //提报需求提交
  toSelect: function (params) {
    return promise(host + 'submitNeedEntity/insertSelective', params, 'POST')
      .then(res => res.data)
  },

  //首页类型
  toExample: function (params) {
    return promise(host + 'sortSettingEntity/selectByExample', params, 'POST')
      .then(res => res.data)
  },


  //优惠卷列表
  couponList: function (params) {
    return promise(host + 'userTicketEntity/getIPage', params, 'POST')
      .then(res => res.data)
  },

  //积分列表
  // integralList: function (params) {
  //   return promise(host + '', params, 'POST')
  //     .then(res => res.data)
  // },

  //积分详情消费列表
  integralDetailsList: function (params) {
    return promise(host + 'userScoreRecordEntity/getIPage', params, 'POST')
      .then(res => res.data)
  },

  //提交订单页面优惠卷
  couponUserList: function (params) {
    return promise(host + 'userTicketEntity/getTicket', params, 'POST')
      .then(res => res.data)
  },

  //首页专场
  toPerformances: function (params) {
    return promise(host + 'goodsSpecEntity/getAllSpec', params, 'POST')
      .then(res => res.data)
  },

  //首页专区
  toSpe: function (params) {
    return promise(host + 'recommendSpecEntity/getByCityId', params, 'POST')
      .then(res => res.data)
  },

  //首页专区详情
  toSpeDetail: function (params) {
    return promise(host + 'goodsInfoEntity/getGoodsBySpecId', params, 'POST')
      .then(res => res.data)
  },

  //首页限时秒杀
  seckill: function (params) {
    return promise(host + 'goodsInfoEntity/getKillGoods', params, 'POST')
      .then(res => res.data)
  },

  //提报详情
  selectDetail: function (params) {
    return promise(host + 'submitNeedEntity/form', params, 'POST')
      .then(res => res.data)
  },

  //切换商家个人
  switchUser: function (params) {
    return promise(host + 'user/getUserRoles', params, 'POST')
      .then(res => res.data)
  },

  //新客专区
  rookieGoods: function (params) {
    return promise(host + 'goodsInfoEntity/getRookieGoods', params, 'POST')
      .then(res => res.data)
  },

  //vip专区
  vipGoods: function (params) {
    return promise(host + 'goodsInfoEntity/getVipGoods', params, 'POST')
      .then(res => res.data)
  },

  PayGetApportion: function (params) {
    return promise(host + 'orderInfoEntity/getApportion', params, 'POST')
      .then(res => res.data)
  },

  // 活动搜索
  activitySearch: function (params) {
    return promise(host + 'goodsSearchEntity/getActiveGoods', params, 'POST')
      .then(res => res.data)
  },

  //个人中心优惠卷数量
  discountUser: function (params) {
    return promise(host + 'userTicketEntity/getSize', params, 'POST')
      .then(res => res.data)
  },

  //优惠搜索
  searchDiscounts: function (params) {
    return promise(host + 'goodsSearchEntity/getActiveGiftGoods', params, 'POST')
      .then(res => res.data)
  },

  //授权列表
  userList: function (params) {
    return promise(host + 'authUserEntity/getIPage', params, 'POST')
      .then(res => res.data)
  },

  //删除授权用户
  userDel: function (params) {
    return promise(host + 'authUserEntity/del', params, 'POST')
      .then(res => res.data)
  },

  //授权店铺列表
  shopList: function (params) {
    return promise(host + 'shopInfoEntity/getShopList', params, 'POST')
      .then(res => res.data)
  },

  //添加店铺
  addShop: function (params) {
    return promise(host + 'authUserEntity/del', params, 'POST')
      .then(res => res.data)
  },

  //获取添加授权列表
  jurisdiction: function (params) {
    return promise(host + 'authUserShopEntity/form', params, 'POST')
      .then(res => res.data)
  },

  //添加店铺
  addShops: function (params) {
    return promise(host + 'authUserShopEntity/save', params, 'POST')
      .then(res => res.data)
  },

  //修改店铺
  modifyShop: function (params) {
    return promise(host + 'authUserShopEntity/edit', params, 'POST')
      .then(res => res.data)
  },

  //获取二维码
  qrCode: function (params) {
    return promise(host + 'qrCode/createLogoQRCode', params, 'POST')
      .then(res => res.data)
  },


  //添加授权人
  empower: function (params) {
    return promise(host + 'authUserEntity/save', params, 'POST')
      .then(res => res.data)
  },

  //佣金
  brokerageList: function (params) {
    return promise(host + 'commissionRecordEntity/getRecord', params, 'POST')
      .then(res => res.data)
  },
  //个人城市选择1
  personChooseAddr: function (keyword) {
    return promise('https://apis.map.qq.com/ws/district/v1/search?&keyword=' + keyword + '&key=' + key, {}, 'GET')
      .then(res => res.data)
  },

  //个人城市选择2
  personChooseAddrTwo: function (keyword) {
    return promise('https://apis.map.qq.com/ws/district/v1/getchildren?&id=' + keyword + '&key=' + key, {}, 'GET')
      .then(res => res.data)
  },

  //个人列表删除
  personDel: function (params) {
    return promise(host + 'addressInfoEntity/del', params, 'POST')
      .then(res => res.data)
  },
  //获取支付方式
  getPickTypeList: function (params) {
    return promise(host + 'shopInfoEntity/getPickTypeList', params, 'POST')
      .then(res => res.data)
  },  
  //清空购物车
  emptyShoppingCart: function (params) {
    return promise(host + 'shopCarEntity/emptyShoppingCart', params, 'POST')
      .then(res => res.data)
  },
}